package com.lzh.persevere.controller;

import cn.hutool.core.io.file.FileNameUtil;
import com.lzh.persevere.common.R;
import com.lzh.persevere.common.ResultUtil;
import com.lzh.persevere.config.MinIOConfig;
import com.lzh.persevere.config.enums.request.ErrorCodeEnum;
import com.lzh.persevere.model.entity.FileInfo;
import com.lzh.persevere.service.FileInfoService;
import com.lzh.persevere.utils.IpUtil;
import com.lzh.persevere.utils.MinIOUtils;
import io.minio.messages.Bucket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "文件模块")
@Slf4j
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Resource
    private MinIOConfig prop;

    @Resource
    private FileInfoService fileInfoService;

    @ApiOperation(value = "查看存储bucket是否存在")
    @GetMapping("/bucketExists")
    public R<Boolean> bucketExists(@RequestParam("bucketName") String bucketName) throws Exception {
        return ResultUtil.success(MinIOUtils.bucketExists(bucketName));
    }

    @ApiOperation(value = "创建存储bucket")
    @GetMapping("/makeBucket")
    public R<Boolean> makeBucket(String bucketName) {
        return ResultUtil.success(MinIOUtils.makeBucket(bucketName));
    }

    @ApiOperation(value = "删除存储bucket")
    @GetMapping("/removeBucket")
    public R<Boolean> removeBucket(String bucketName) throws Exception {
        return ResultUtil.success(MinIOUtils.removeBucket(bucketName));
    }

    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public R<List<Bucket>> getAllBuckets() throws Exception {
        return ResultUtil.success(MinIOUtils.getAllBuckets());
    }

    @ApiOperation(value = "文件上传返回url")
    @PostMapping("/upload")
    public R<?> upload(@RequestPart("file") MultipartFile file, @RequestParam("moduleId") String moduleId) {
        FileInfo fileInfo = new FileInfo();
        String objectName = MinIOUtils.upload(file, fileInfo);
        if (null != objectName) {
            String fileAddress = prop.getEndpoint() + "/" + prop.getBucketName() + "/" + objectName;
            String originalFilename = file.getOriginalFilename();

            fileInfo.setModuleId(moduleId);
            fileInfo.setOriginName(originalFilename);
            fileInfo.setFileAddress(fileAddress);
            fileInfo.setFileSize(file.getSize());
            fileInfo.setFileType(FileNameUtil.getSuffix(originalFilename));
            fileInfo.setIp(IpUtil.getIpAddress());
            fileInfoService.save(fileInfo);
            return ResultUtil.success(fileAddress);
        }
        return ResultUtil.error(ErrorCodeEnum.OPERATION_ERROR);
    }
    
    @ApiOperation(value = "文件上传返回文件信息")
    @PostMapping("/upload1")
    public R<?> upload1(@RequestPart("file") MultipartFile file, @RequestParam("moduleId") String moduleId) {
        FileInfo fileInfo = new FileInfo();
        String objectName = MinIOUtils.upload(file, fileInfo);
        if (null != objectName) {
            String fileAddress = prop.getEndpoint() + "/" + prop.getBucketName() + "/" + objectName;
            String originalFilename = file.getOriginalFilename();

            fileInfo.setModuleId(moduleId);
            fileInfo.setOriginName(originalFilename);
            fileInfo.setFileAddress(fileAddress);
            fileInfo.setFileSize(file.getSize());
            fileInfo.setFileType(FileNameUtil.getSuffix(originalFilename));
            fileInfo.setIp(IpUtil.getIpAddress());
            fileInfoService.save(fileInfo);
            return ResultUtil.success(fileInfo);
        }
        return ResultUtil.error(ErrorCodeEnum.OPERATION_ERROR);
    }


    @ApiOperation(value = "文件上传返回文件信息 - 压缩图片")
    @PostMapping("/upload2")
    public R<?> upload2(@RequestPart("file") MultipartFile file, @RequestParam("moduleId") String moduleId) {
        FileInfo fileInfo = new FileInfo();
        String objectName = MinIOUtils.upload2(file, fileInfo);
        if (null != objectName) {
            String fileAddress = prop.getEndpoint() + "/" + prop.getBucketName() + "/" + objectName;
            String originalFilename = file.getOriginalFilename();

            fileInfo.setModuleId(moduleId);
            fileInfo.setOriginName(originalFilename);
            fileInfo.setFileAddress(fileAddress);
            fileInfo.setFileSize(file.getSize());
            fileInfo.setFileType(FileNameUtil.getSuffix(originalFilename));
            fileInfo.setIp(IpUtil.getIpAddress());
            fileInfoService.save(fileInfo);
            return ResultUtil.success(fileInfo);
        }
        return ResultUtil.error(ErrorCodeEnum.OPERATION_ERROR);
    }

    @ApiOperation(value = "图片/视频预览")
    @GetMapping("/preview")
    public R<String> preview(@RequestParam("fileName") String fileName) {
        return ResultUtil.success(MinIOUtils.preview(fileName));
    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public R<?> download(@RequestParam("fileName") String fileName, HttpServletResponse res) {
        MinIOUtils.download(fileName, res);
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "删除文件", notes = "根据url地址删除文件")
    @PostMapping("/delete")
    public R<String> remove(String url) {
        String objName = url.substring(url.lastIndexOf(prop.getBucketName() + "/") + prop.getBucketName().length() + 1);
        MinIOUtils.remove(objName);
        return ResultUtil.success(objName);
    }

    @ApiOperation(value = "下载所有文件", notes = "下载所有文件")
    @PostMapping("/downloadAllFiles")
    public R<String> downloadAllFiles() throws Exception {
        MinIOUtils.downloadAllFiles("persevere-lzh",null, "/Users/luzhiheng/Downloads");
        return ResultUtil.success(null);
    }

}
